package com.haiyi.software.app.basic;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.os.Bundle;

public abstract class ActivityMySayBasic extends ActivityBasic {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	protected void updUserSign(String str_say) {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

		Map<String, Object> validator = new HashMap<String, Object>();

		validator.put("uid", myInfo.getUid());
		validator.put("uname", myInfo.getUname());
		validator.put("pwd", myInfo.getPwd());
		validator.put("uguid", myInfo.getUguid());

		Map<String, Object> argsBody = new HashMap<String, Object>();
		argsBody.put("sign", str_say);

		Map<String, Object> args = new HashMap<String, Object>();
		args.put("validator", validator);
		args.put("body", argsBody);

		Gson gson = new Gson();

		String s = gson.toJson(args);

		requestNetworkData(s);
	}

	@Override
	protected void requestNetworkData(Object... args) {

		String str_requestURL = str_server + "updUserSign.php";

		AsyncHttpClient client = MainApplication.getInstance()
				.getAsyncHttpClient();

		RequestParams params = new RequestParams();

		params.put("args", args[0].toString());

		client.post(str_requestURL, params, responseHandler);
	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			try {
				String str_Return = new String(responseBody, "UTF-8");

				L.i("updUserSign=" + str_Return);

				Gson gson = new Gson();

				JsonParser parser = new JsonParser();

				JsonObject jsonResult = parser.parse(str_Return)
						.getAsJsonObject();

				requestFinish(jsonResult);

			} catch (Exception e) {

				L.e(e.getMessage());
				requestError();
			}

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {

			L.e(error.getMessage());

			requestError();
		}
	};
}
